package com.binhthuan.catechist.client.viewer;

import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.data.PersonDS;
import com.binhthuan.catechist.shared.constant.Parameter;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.XMLTools;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.viewer.DetailFormatter;
import com.smartgwt.client.widgets.viewer.DetailViewerField;

public class PersonViewer extends AbstractViewer {

    private int rowHeight = 21;
    private int headerHeight = 23;
    private Img image;
    private DSCallback callBack = new DSCallback() {

        @Override
        public void execute(DSResponse response, Object rawData, DSRequest request) {
            String imageUrl = XMLTools.selectObjects(rawData, Parameter.AVATAR).getJavaScriptObject().toString();
            if (imageUrl == null || imageUrl.trim().length() <= 0) {
                imageUrl = "images/layout/no-avatar.png";
            }
            setImage("[APP]/" + imageUrl);
        }
    };

    public PersonViewer(PersonDS dataSource) {
        super(dataSource);

        DetailViewerField personalHeader = new DetailViewerField();
        personalHeader.setType(HEADER);
        personalHeader.setValue(ECApplication.getConstants().personalInformation());

        DetailViewerField advanceHeader = new DetailViewerField();
        advanceHeader.setType(HEADER);
        advanceHeader.setValue(ECApplication.getConstants().advanceInformation());

        DetailViewerField[] personalFields = getPersonalFields();
        DetailViewerField[] advanceFields = getAdvanceFields();
        int size = 0;
        int height = 0;
        if (personalFields != null && personalFields.length > 0) {
            size += 1 + personalFields.length;
        }
        if (advanceFields != null && advanceFields.length > 0) {
            size += 1 + advanceFields.length;
        }

        DetailViewerField[] fields = new DetailViewerField[size];
        int index = 0;
        if (personalFields != null && personalFields.length > 0) {
            fields[index++] = personalHeader;
            height += headerHeight;
            for (DetailViewerField f : personalFields) {
                fields[index++] = f;
                height += rowHeight;
            }
        }

        if (advanceFields != null && advanceFields.length > 0) {
            fields[index++] = advanceHeader;
            height += headerHeight;
            for (DetailViewerField f : advanceFields) {
                fields[index++] = f;
                height += rowHeight;
            }
        }
        setFields(fields);
        image = new Img();
        image.setSize(height);
    }

    public DetailViewerField[] getPersonalFields() {
        DetailViewerField patronSaint = new DetailViewerField(Parameter.PATRON_SAINT);
        patronSaint.setTitle(ECApplication.getConstants().patronSaint());

        DetailViewerField fullName = new DetailViewerField(Parameter.FULL_NAME);
        fullName.setTitle(ECApplication.getConstants().fullName());

        DetailViewerField genderField = new DetailViewerField(Parameter.MALE);
        genderField.setTitle(ECApplication.getConstants().gender());
        genderField.setDetailFormatter(new DetailFormatter() {

            @Override
            public String format(Object value, Record record, DetailViewerField field) {
                boolean isMale = (value == null) ? false : Boolean.valueOf(value.toString());
                String gender = isMale ? ECApplication.getConstants().male() : ECApplication.getConstants().female();
                return gender;
            }
        });

        DetailViewerField birthday = new DetailViewerField(Parameter.BIRTHDAY);
        birthday.setTitle(ECApplication.getConstants().birthday());

        DetailViewerField phone = new DetailViewerField(Parameter.PHONE);
        phone.setTitle(ECApplication.getConstants().phone());

        return new DetailViewerField[] { patronSaint, fullName, genderField, birthday, phone };
    }

    public DetailViewerField[] getAdvanceFields() {
        return null;
    }

    public Canvas createCanvas() {
        final HLayout layout = new HLayout();
        layout.setMembers(this, image);

        return layout;
    }

    public void setImage(String src) {
        image.setSrc(src);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.smartgwt.client.widgets.viewer.DetailViewer#fetchData(com.smartgwt
     * .client.data.Criteria)
     */
    @Override
    public void fetchData(Criteria criteria) {
        super.fetchData(criteria, callBack);
    }
}
